<?php
namespace CouchPHP;

/**
 * Server utils
 * http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference
 * @package CouchPHP
 * @author Jakub `Kofel` Kaflik
 */
class Server
{
    /**
     * Server connection object
     * @var \CouchPHP\Connection
     */
    public $connection;
    
    /**
     * Server configuration object
     * @var \CouchPHP\Config
     */
    public $config;
    
    public $version;
    
    /**
     * Constructor
     * @param string $host
     * @param int $port
     * @param callback $connection
     */
    public function __construct( $host, $port = 5984, $connection = null )
    {
        if( !$connection )
        {
            $connection = function( $host, $port ){ return new Connection( $host, $port ); };
        }
        
        $this->connection = $connection( $host, $port );
        
        $this->version = $this->connection->doRequest('/')->json()->version;
    }
    
    /**
     * Lists all databases
     * @return array
     */
    public function listDatabases()
    {
        return $this->connection->doRequest('/_all_dbs/')->json();
    }
    
    /**
     * Lists server statistics
     * @return array
     */
    public function statistics()
    {
        return $this->connection->doRequest('/_stats/')->json();
    }
}